﻿#ifndef ONCE_TRIGGER_THREAD_H
#define ONCE_TRIGGER_THREAD_H

#include "libdaq/device/base_device.h"
#include <thread>
#include <vector>
#include <atomic>
#include <chrono>
#include <memory>
#include <iostream>
#include <condition_variable>

// 使用std::chrono代替QElapsedTimer
// 使用std::mutex和std::condition_variable代替信号槽机制

class OnceTriggerADCThread
{
public:
  OnceTriggerADCThread(std::shared_ptr<libdaq::device::LockzhinerBaseDevice> base_device)
      : base_device_(base_device), thread_is_running_(false) {}

  void start();
  void quit();
  void run();
  bool thread_is_running();

  void setThread_is_running(bool newThread_is_running);

  private:
  bool ReadADCData();
  bool CheckTimeout(const std::chrono::steady_clock::time_point &start, int timeout_msec);
  bool CheckDataEmpty();

  std::weak_ptr<libdaq::device::LockzhinerBaseDevice> base_device_;
  std::atomic<bool> thread_is_running_;
  std::thread thread_;

  int total_data_length_;
  int timeout_msec_;
  std::vector<std::vector<float>> data_;

  std::mutex mutex_;
  std::condition_variable cv_;
};

#endif // ONCE_TRIGGER_THREAD_H
